class Compiler {
  constructor(vm) {
    this.el = vm.$el;
    this.options = vm.$options;
    this.vm = vm;
    this.compile(this.el);
  }
  // 编译模版，处理节点
  compile(el) {
    let childNodes = el.childNodes;

    Array.from(childNodes).forEach(node => {
      if (this.isTextNode(node)) {
        this.compileText(node);
      } else if (this.isElementNode(node)) {
        this.compileElement(node);
      }

      // 判断node节点，如果存在子节点，递归compile
      if (node.childNodes && node.childNodes.length) {
        this.compile(node);
      }
    })
  }

  // 编译元素节点，处理指令
  compileElement(node) {
    Array.from(node.attributes).forEach(attr => {
      let attrName = attr.name;
      if (this.isDirective(attrName)) {
        attrName = attrName.substr(2);
        let key = attr.value;
        this.update(node, key, attrName)
      }
    })
  }

  update(node, key, attrName) {
    let isOn = attrName.indexOf('on:') === 0;
    let updateFn = this[`${isOn ? 'event' : attrName}Updater`];
    updateFn && updateFn.call(this, node, isOn ? attrName.substr(3) : this.vm[key], key)
  }

  /**
   * 实现指令v-text
   * @param {*} node 
   * @param {*} value 
   * @param {*} key 
   */
  textUpdater(node, value, key) {
    node.textContent = value;
    new Watcher(this.vm, key, (newValue) => {
      node.textContent = newValue;
    })
  }

  /**
   * 实现指令v-model
   * @param {*} node 
   * @param {*} value 
   * @param {*} key 
   */
  modelUpdater(node, value, key) {
    node.value = value;
    new Watcher(this.vm, key, (newValue) => {
      node.value = newValue;
    });
    // 表单元素添加双向绑定
    node.addEventListener('input', () => {
      this.vm[key] = node.value;
    })
  }

  /**
   * 实现指令v-htm
   * @param {*} node 
   * @param {*} value 
   * @param {*} key 
   */
  htmlUpdater(node, value, key) {
    node.innerHTML = value;
    new Watcher(this.vm, key, (newValue) => {
      node.innerHTML = newValue;
    });
  }

  /**
   * 实现指令v-on
   * @param {*} node 
   * @param {*} value 
   * @param {*} key 
   */
  eventUpdater(node, value, key) {
    let reg = /(.+?)\((.+?)\)/;
    let eventKey = key;
    let eventParams;
    let eventNode = false;
    if (reg.test(key)) {
      eventKey = RegExp.$1.trim();
      const [fir, sec] = RegExp.$2.replace(/\'|\"/g, '').split(',');
      eventParams = fir.trim();
      eventNode = sec && sec.indexOf('$event') > -1;
    }
    console.log(eventNode)
    const targetFn = this.options.methods[eventKey];
    node.addEventListener(value, (e) => {
      targetFn && targetFn.call(this.vm, eventParams, eventNode ? e : undefined);
    })
  }

  // 编译文本节点，处理差值表达式
  compileText(node) {
    // console.dir(node);
    let reg = /\{\{(.+?)\}\}/;
    let value = node.textContent;
    if (reg.test(value)) {
      let key = RegExp.$1.trim();
      node.textContent = value.replace(reg, this.vm[key]);

      // 创建watcher对象，当数据改变更新视图
      new Watcher(this.vm, key, (newValue) => {
        node.textContent = newValue;
      })
    }
  }

  // 判断元素属性是否有指令
  isDirective(attrName) {
    return attrName.startsWith('v-')
  }

  // 判断节点是否为文本节点
  isTextNode(node) {
    return node.nodeType === 3;
  }

  // 判断节点是否为元素节点
  isElementNode(node) {
    return node.nodeType === 1;
  }
}